package org.dei.perla.sys.device.plugnplay.plugnplaymanager;

import java.io.File;
import java.io.Reader;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.JAXBContext;

import org.dei.perla.sys.device.plugnplay.xsdgeneratedclasses.*;

public class ClassTreeManager {
	private ParameterManager pm;
	
	public ClassTreeManager(){
		pm = new ParameterManager();
	}
	public ParameterManager getParameterManager (){
		return this.pm;
	}
	
	public PerlaDevice createClassTree(String pkg, String xml) throws Exception {
		JAXBContext jc = JAXBContext.newInstance(pkg);
		Unmarshaller unmarshaller = jc.createUnmarshaller();
		PerlaDevice pd = (PerlaDevice) ((JAXBElement) unmarshaller
				.unmarshal(new File(xml))).getValue();
		return pd;
	}
	
	public PerlaDevice createClassTree(String pkg, Reader xmlReader) throws Exception {
		JAXBContext jc = JAXBContext.newInstance(pkg);
		Unmarshaller unmarshaller = jc.createUnmarshaller();
		PerlaDevice pd = (PerlaDevice) ((JAXBElement) unmarshaller.unmarshal(xmlReader)).getValue();
		return pd;
	}
	
	private void manageTree(PerlaDevice pd){
		//ParameterManager pm=new ParameterManager(pd);
		pm.printDevice();
		pm.toFile();
		pm.printLogFile();
	}
	public void manageTree(PerlaDevice pd, String fileDir){
		this.pm=new ParameterManager(pd);
		if (fileDir==null)
			manageTree(pd);
		else {
			pm.setFileDir(fileDir);
			manageTree(pd);
		}
	}
	
	public void createJavaFiles(String pkg, String xml, String fileDir) throws Exception{
		manageTree(createClassTree(pkg,xml),fileDir);
	}
	
	public void createJavaFiles(String pkg, Reader xmlReader, String fileDir) throws Exception{
		manageTree(createClassTree(pkg, xmlReader),fileDir);
	}
	
	
	public static void main(String[] args){
		ClassTreeManager ctm = new ClassTreeManager();
		try{
			ctm.createJavaFiles("org.dei.perla.sys.device.plugnplay.xsdgeneratedclasses", "testDevice.xml", null);
		}catch(Exception e){e.printStackTrace();}
	}
	
	
	

	
}
